import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class InputTest extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _InputTest();
  }
}

class _InputTest extends State<InputTest> with SingleTickerProviderStateMixin {
  TextEditingController _unameController = TextEditingController();
  FocusNode focusNode1 = new FocusNode();
  FocusNode focusNode2 = new FocusNode();
  FocusScopeNode focusScopeNode = new FocusScopeNode();

  TextEditingController _unameController1 = TextEditingController();
  TextEditingController _passController1 = TextEditingController();
  GlobalKey _formKey = GlobalKey<FormState>();
  AnimationController _animationController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _unameController.addListener(() {});
    _animationController =
        AnimationController(vsync: this, duration: Duration(seconds: 3));
    _animationController.forward();
    _animationController.addListener(() => setState(() => {}));
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return WillPopScope(
        child: Scaffold(
          appBar: AppBar(
            title: Text('输入库测试界面'),
          ),
          body: Scrollbar(
            child: SingleChildScrollView(
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.all(20),
                    child: TextField(
                      autofocus: true,
                      focusNode: focusNode1,
                      controller: _unameController,
                      style: TextStyle(
                        color: Colors.red,
                      ),
                      decoration: InputDecoration(
                        prefixIcon: Icon(Icons.person),
                        contentPadding: EdgeInsets.all(10),
                        labelText: "用户名",
                        hintText: '用户名或邮箱',
                        focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.red)),
                        enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.red)),
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(20),
                    child: Column(
                      children: <Widget>[
                        TextField(
                          focusNode: focusNode2,
                          style: TextStyle(color: Colors.red),
                          obscureText: true,
                          decoration: InputDecoration(
                            prefixIcon: Icon(Icons.lock),
                            labelText: '密码',
                            hintText: '您的输入密码',
                            border: OutlineInputBorder(),
                            contentPadding: EdgeInsets.all(10),
                          ),
                        ),
                      ],
                    ),
                  ),
                  FlatButton(
                    onPressed: () {
                      focusNode1.unfocus();
                      focusNode2.unfocus();
                    },
                    child: Text("隐藏键盘"),
                  ),
                  Divider(
                    height: 1.0,
                    indent: 0.0,
                    color: Colors.red,
                  ),
                  Padding(
                    padding: EdgeInsets.symmetric(vertical: 15, horizontal: 20),
                    child: Form(
                      key: _formKey,
                      autovalidate: true,
                      child: Column(
                        children: <Widget>[
                          Padding(
                            padding: EdgeInsets.all(5),
                            child: TextFormField(
                              autofocus: true,
                              controller: _unameController1,
                              decoration: InputDecoration(
                                prefixIcon: Icon(Icons.person),
                                labelText: '用户名',
                                hintText: '用户名或邮箱',
                                border: OutlineInputBorder(),
                                contentPadding: EdgeInsets.all(10),
                              ),
                              validator: (v) {
                                return v.trim().length > 0 ? null : '用户名不能为空';
                              },
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.all(5),
                            child: TextFormField(
                              controller: _passController1,
                              decoration: InputDecoration(
                                labelText: '密码',
                                hintText: '请输入密码',
                                border: OutlineInputBorder(),
                                contentPadding: EdgeInsets.all(10),
                                prefixIcon: Icon(Icons.lock),
                              ),
                              obscureText: true,
                              validator: (v) {
                                return v.trim().length > 5 ? null : '密码不能小于6位';
                              },
                            ),
                          ),
                          Row(
                            children: <Widget>[
                              Expanded(
                                child: Padding(
                                  padding: EdgeInsets.all(20),
                                  child: RaisedButton(
                                    child: Text("登录"),
                                    color: Theme.of(context).primaryColor,
                                    onPressed: () {
                                      if ((_formKey.currentState as FormState)
                                          .validate()) {
                                        Fluttertoast.showToast(msg: '登录成功');
                                      } else {
                                        Fluttertoast.showToast(msg: '数据无效');
                                      }
                                    },
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.symmetric(vertical: 20, horizontal: 0),
                    child: LinearProgressIndicator(
                      backgroundColor: Colors.green[100],
                      valueColor:
                          ColorTween(begin: Colors.grey, end: Colors.blue)
                              .animate(_animationController), // 从灰色变成蓝色
                      value: _animationController.value,
                    ),
                  ),
                  LinearProgressIndicator(
                    backgroundColor: Colors.red[200],
                    valueColor: AlwaysStoppedAnimation(Colors.blue),
                    value: .52,
                  )
                ],
              ),
            ),
          ),
        ),
        onWillPop: null);
  }
}
